package com.example.testrecorder;

import java.io.File;
import java.io.IOException;
import java.util.Currency;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.util.Printer;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.os.Build;

public class Recording extends Activity {
	private MediaRecorder recorder;
	private MediaPlayer player;
	private String OUTPUT_FILE;
	private static final String AUDIO_RECORDER_FOLDER = "AudioRecorder";
	//private static final String AUDIO_RECORDER_FILE_EXT_3GP = ".3gp";
	private static final String AUDIO_RECORDER_FILE_EXT_MP4 = ".mp4";
	private String file_exts = AUDIO_RECORDER_FILE_EXT_MP4;
	
	

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_);
       
        
        				
        recorder = new MediaRecorder();
        player = new MediaPlayer();

    }
    
    public void doClick(View view) {
        switch(view.getId()) {
        case R.id.btnStart:
        try {
        startRecording();
        } catch (Exception e) {
        e.printStackTrace();
        }
        break;
        case R.id.btnStop:
        try {
        stopRecording();
        } catch (Exception e) {
        e.printStackTrace();
        }
        break;
        case R.id.btnPlayRecording:
            try {
            playRecording();
            } catch (Exception e) {
            e.printStackTrace();
            }
            break;
            case R.id.btnStopRecording:
            try {
            stopPlayingRecording();
            } catch (Exception e) {
            e.printStackTrace();
            }
            break;
            case R.id.btnChoose:
            try{
            	chooseFile();
            }
            catch(Exception e){
            	e.printStackTrace();
           
            }
            }
    }
    public void startRecording(){
    	recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
		recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
		recorder.setOutputFile(getFilename());
		recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
		try {
			recorder.prepare();
			recorder.start();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
    }
    
    public void stopRecording(){
    	if (null != recorder) {
			recorder.stop();
			recorder.reset();
			recorder.release();
			recorder = null;

    }
}
  
    public void playRecording() throws IllegalArgumentException, SecurityException, IllegalStateException, IOException{
    	 player.setDataSource(getFilename());
    	 player.prepare();
    	 player.start();
    }
    
    public void stopPlayingRecording(){
        if(player != null) {
            player.stop();
            }   
        }
    public String getFilename(){
    	OUTPUT_FILE = Environment.getExternalStorageDirectory().getAbsolutePath();
        File file = new File(OUTPUT_FILE, AUDIO_RECORDER_FOLDER);
        if (!file.exists()) {
			file.mkdirs();
		}
        
        return (file.getAbsolutePath() + "/" + System.currentTimeMillis()+ file_exts);
    }
    
    public void chooseFile(){
    	
    }
}